package com.park.web.controller.pub;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.client.model.pub.SensitiveWordModel;
import com.park.client.model.question.QuestionAnswerModel;
import com.park.client.model.question.QuestionModel;
import com.park.client.model.question.QuestionOperationModel;
import com.park.client.model.question.QuestionTypeModel;
import com.park.client.model.user.UserModel;
import com.park.client.service.pub.SensitiveWordService;
import com.park.client.service.question.QuestionAnswerService;
import com.park.client.service.question.QuestionOperationService;
import com.park.client.service.question.QuestionService;
import com.park.client.service.question.QuestionTypeService;
import com.park.common.util.Page;
import com.park.common.util.Result;
import com.park.web.contant.Global;
import com.park.web.controller.BaseController;
import com.park.web.util.SessionUtil;

/**
 * @author Administrator
 *问题交流
 */
@Controller
@RequestMapping("/public/question")
public class PublicQuestionController extends BaseController{

	@Autowired
	private QuestionService questionService;
	@Autowired
	private QuestionTypeService questionTypeService;
	@Autowired
	private QuestionAnswerService questionAnswerService;
	@Autowired
	private QuestionOperationService questionOperationService;
	@Autowired
	private SensitiveWordService sensitiveWordService;
	
	@RequestMapping("/questionTypeList/{id}")
	public String questionTypeList(@PathVariable("id") Long id,Integer pageNow,HttpServletRequest request){
			QuestionModel question = new QuestionModel();
			question.setQuestionTypeId(id);
			Integer total = questionService.selectTotal(question);
			Page page = new Page(pageNow,total,10);
			question.setStartPos(page.getStartPos());
			List<QuestionModel> questions = questionService.selectPagination(question);
			request.setAttribute("total", total);
			request.setAttribute("page", page);
			request.setAttribute("questions", questions);
			
			QuestionTypeModel questionType = questionTypeService.selectByPrimaryKey(id);
			request.setAttribute("questionTypes", Arrays.asList(questionType));
			return Global.PUBLIC_PATH+"question/questionTypeList";
	}
	
	
	/**
	 *单个问题详情 
	 */
	@RequestMapping("/detail/{id}")
	public String detail(@PathVariable("id") Long id,Integer pageNow,HttpServletRequest request){
		QuestionModel question = questionService.selectByPrimaryKey(id);
		 question.setQuestionClick(question.getQuestionClick()+1);
		 questionService.updateByPrimaryKeySelective(question);
		request.setAttribute("question", question);
		
		//问题回答展示
		QuestionAnswerModel questionAnswer= new QuestionAnswerModel();
		questionAnswer.setQuestionId(id);
		Integer total = questionAnswerService.selectTotal(questionAnswer);
		request.setAttribute("totalAnswers", total);//对应问题的回答总数
//		Page page = new Page(pageNow !=null?pageNow:1,total,10);
		List<QuestionAnswerModel> questionAnswers=questionAnswerService.selectPagination(questionAnswer);
		request.setAttribute("questionAnswers", questionAnswers);
		
		//问题分类
		QuestionTypeModel questionType = questionTypeService.selectByPrimaryKey(question.getQuestionTypeId());
		request.setAttribute("questionType", questionType);
		
		return Global.PUBLIC_PATH+"question/questionDetail";
	}
	
	/**
	 *单个问题回答 
	 */
	@RequestMapping("questionAnswerDetail")
	public String questionAnswerDetail(@RequestParam("id") Long id,HttpServletRequest request){
		QuestionModel question = questionService.selectByPrimaryKey(id);//当前页
		request.setAttribute("question", question);
		return Global.PUBLIC_PATH+"question/questionDetail";
	}
	
	/**
	 * 问题收藏、关注
	 */
	@RequestMapping("questionOperation")
	@ResponseBody
	public Result<QuestionModel> questionOperation(@RequestParam("id")Long id,@RequestParam("type")Long type,HttpSession session){
		Result<QuestionModel> result = new Result<>(true);
		UserModel user = SessionUtil.getSessionUser(session);
		if(user != null){
			QuestionModel question  = questionService.selectByPrimaryKey(id);
			QuestionOperationModel qo = new QuestionOperationModel();
			qo.setUserId(user.getId());
			qo.setQuestionId(id);
			qo.setOperationType(type);
			QuestionOperationModel q = questionOperationService.selectByOperation(qo);
			if(q != null){
				result.setSuccess(false);
				result.setCode(103);
				result.setMessage("你已经"+(type==1?"关注":"收藏")+"过，请勿重复操作!");
			}else{
					qo.setCreateDate(new Date());
					questionOperationService.insertSelective(qo);
					if(type ==2){//收藏
						question.setQuestionCollection(question.getQuestionCollection()+1);
					}else{
						question.setQuestionAbout(question.getQuestionAbout()+1);
					}
					boolean success= questionService.updateByPrimaryKeySelective(question);
					if(success){
						question  = questionService.selectByPrimaryKey(id);
						result.setObj(question);
						result.setSuccess(true);
						result.setCode(100);
						result.setMessage("恭喜你,"+(type==1?"关注":"收藏")+"成功!");
					}else{
						result.setSuccess(false);
						result.setMessage("对不起，你"+(type==1?"关注":"收藏")+"失败!");
						result.setCode(101);
					}
			}
		}else{
			result.setSuccess(false);
			result.setCode(102);
			result.setMessage("你还未登录，请登录后再操作!");
		}
		return result;
	}
	
	/**
	 * 回答提问
	 */
	@RequestMapping("questionAnswer")
	@ResponseBody
	public Result<QuestionAnswerModel> questionAnswer(@RequestParam("questionAnswer") String questionAnswer,@RequestParam("questionId") Long questionId,HttpServletRequest request,HttpSession session){
		Result<QuestionAnswerModel> json = new Result<>(true);
		UserModel user = SessionUtil.getSessionUser(session);
		if(user != null){
			QuestionAnswerModel qa = new QuestionAnswerModel();
			qa.setAnswerUserId(user.getId());
			qa.setQuestionId(questionId);
			QuestionAnswerModel answer = questionAnswerService.selectIsExists(qa);
			if(answer == null){
				//过滤敏感词
				List<SensitiveWordModel> words = sensitiveWordService.selectAll();
				for (SensitiveWordModel word : words) {
					questionAnswer = questionAnswer.replaceAll(word.getWord(), "***");
				}
				qa.setQuestionAnswer(questionAnswer);
				qa.setCreateDate(new Date());
				boolean success = questionAnswerService.insertSelective(qa);
				String msg= "回答成功!";
				if(success){
					json.setSuccess(success);
					//回答数量加1
					QuestionModel question = questionService.selectByPrimaryKey(questionId);
					question.setQuestionAnswerCount(question.getQuestionAnswerCount()+1);
					questionService.updateByPrimaryKeySelective(question);
				}else{
					msg = "回答失败!";
					success = false;
				}
				json.setObj(qa);
				json.setSuccess(success);
				json.setMessage(msg);
			}else{
				json.setSuccess(false);
				json.setMessage("你已回答过，请勿重复回答!");
			}
		}else{
			json.setSuccess(false);
			json.setMessage("对不起，请登陆之后再回答!");
			json.setCode(102);
		}
		return json;
	}
	
}
